package com.geoboard.Fragments;

import com.geoboard.FragInterCommunication;
import com.geoboard.FragInterCommunication.State;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.locoboard.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PostsMapFrag extends Fragment{

	/**** Android Callback Methods *********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		//getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		
		SupportMapFragment mapFragment = ((FragInterCommunication)getActivity()).getMapFrag();
		FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();		
		
		transaction.add(R.id.map_posts_container,mapFragment);
		transaction.addToBackStack("map_posts");
		transaction.commit();
		
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		// add the map
				
		
		
		
		
		
		
		FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.post_txtbox_container, new TextPostFrag());
		transaction.commit();
			
		
		return inflater.inflate(R.layout.frag_posts_map, container, false);
		
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated (Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		
	}
	
	@Override
	public void onViewStateRestored (Bundle savedInstanceState){
		super.onViewStateRestored(savedInstanceState);
	}
	
	@Override
	public void onStart(){
		super.onStart();
		
		(getActivity().findViewById(R.id.post_txtbox_container)).bringToFront();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		((FragInterCommunication)getActivity()).setCurrentState(State.POSTS_MAP);
		
		// add the map
		SupportMapFragment mapFragment = ((FragInterCommunication)getActivity()).getMapFrag();
		
		FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();		
		
		transaction.replace(R.id.map_posts_container,mapFragment);
		//transaction.addToBackStack("map_posts");

		transaction.commit();
		
		(getActivity().findViewById(R.id.post_txtbox_container)).bringToFront();
	}
		
		
	
	@Override
	public void onPause(){
		super.onPause();
		
		//getActivity().getSupportFragmentManager().popBackStack("map_posts", 0);
		
		// remove map
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(((FragInterCommunication)getActivity()).getMapFrag());
       
        ft.commit();
			
	}
	
	@Override
	public void onStop(){
		super.onStop();
	}
	
	@Override
	public void onDestroyView(){
		super.onDestroyView();
		
		
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override 
	public void onDetach(){
		super.onDetach();
	}

}
